import java.util.ArrayList;
import java.util.Date;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {

        ArrayList<WebPage> list = new ArrayList<>();

        WebPage page1 = new WebPage("www.baidu.com", 0, new Date());
        WebPage page2 = new WebPage("www.bilibili.com", 0, new Date());
        WebPage page3 = new WebPage("www.douban.com", 0, new Date());

        list.add(page1);
        list.add(page2);
        list.add(page3);

        System.out.println("选择你想进行的操作：");
        System.out.println("1.访问网页\n2.查看访问量排行\n3.查看总访问量\n4.查看网页平均访问量\n5.查看某页面最后访问时间\n6.清空访问记录\n7.结束");

        while (true) {
            Scanner scanner = new Scanner(System.in);
            int choice = scanner.nextInt();
            switch (choice) {
                case 1:
                    visit(list);
                    System.out.println("再次选择操作");
                    break;
                case 2:
                    rank(list);
                    System.out.println("再次选择操作");
                    break;
                case 3:
                    sum(list);
                    System.out.println("再次选择操作");
                    break;

                case 4:
                    average(list);
                    System.out.println("再次选择操作");
                    break;
                case 5:
                    time(list);
                    System.out.println("再次选择操作");
                    break;
                case 6:
                    delete(list);
                    System.out.println("再次选择操作");
                    break;
                case 7:
                    exit();
                    System.out.println("再次选择操作");
                    break;
                default:
                    break;
            }

        }
    }

    public  static void  visit(ArrayList<WebPage> list){
        Scanner scanner = new Scanner(System.in);
        System.out.println("输入网址：");
        String url = scanner.next();
        int a = 0;
        for (int i = 0; i < list.size(); i++) {
            if (url.equals(list.get(i).getUrl())) {
                list.get(i).lastCount();
                a = 1;
                break;
            }
        }

        if (a == 0) {
            System.out.println("未发现此网址，请稍后重试");
            list.add(new WebPage(url, 0, new Date()));

        }
    }


    public static void rank(ArrayList<WebPage> list){
        list.sort((p1, p2) -> Integer.compare(p2.getPageCount(), p1.getPageCount()));

        System.out.println("访问量排行：");
        for (int i = 0; i < list.size(); i++) {
            WebPage p = list.get(i);
            System.out.println(p.getUrl());
        }

    }

    public static void sum(ArrayList<WebPage> list){
        int total = 0;
        for (int i = 0; i < list.size(); i++) {
            total += list.get(i).getPageCount();
        }
        System.out.println("总访问量: " + total);
    }

    public static void  average(ArrayList<WebPage> list){
        double total = 0;
        double average = 0.0;
        for (int i = 0; i < list.size(); i++) {
            total += list.get(i).getPageCount();
        }
        average = total / list.size();
        System.out.println("平均访问量: " + average);

    }

    public static void  time(ArrayList<WebPage> list){
        Scanner scanner = new Scanner(System.in);
        System.out.println("你想要查看哪个网址的最终访问时间？");
        String choiceUrl = scanner.next();
        for (int i = 0; i < list.size(); i++) {
            if(choiceUrl.equals(list.get(i).getUrl())){

                System.out.println("该网页的最终访问时间为："+list.get(i).getDate());
                break;
            }
        }
    }

    public static void delete(ArrayList<WebPage> list){
        for (int i = 0; i < list.size(); i++) {
            WebPage p = list.get(i);
            p.setPageCount(0);

        }
        System.out.println("已清空");
    }

    public static void exit(){
        System.exit(0);
    }
}